<!-- UserModel.php-->
<?php 
/*	require '../common/ConnDB.php';
	require '../common/config.php';*/
	//dirname(__FILE__) 一般会返回文件所在当前目录到系统根目录的一个目录结构。
	require 'Model.php';
	class UserModel extends Model{
		function __Construct(){
			parent::__Construct();
		}
		function getUserList(){
			$sql = 'select * from users';
			$data = $this->conn->query($sql);
			//$this->conn = new ConnDB();
			return $data;
		}

		function getUserByID($id){
			$sql = "select * from users where uid=".$id;
			$data = $this->conn->query($sql);
			return $data;
		}

		function getuserinfor($uname){
			$sql = "select * from users where uname like '%".$uname."%' limit 0,5 ";;
			$data = $this->conn->query($sql);
			return $data;
		}

		function Updateuser($data){
			if(!empty($_FILES["pic"]["name"])){
  				$pic = parent::uploadfile('pic','../images/',array('png','jpg','gif'));
  			}
		    else{
		  		$pic='';
		  	}
		  	$user = array(
				'uid' =>$_POST['id'],		
				'uname' => trim($_POST['uname']),  
				'gender' => $_POST['gender'],
				'birthdate' => $_POST['birthdate'],
				'hobby' => implode($_POST['hobby'], ','),
				'degree' => $_POST['degree'],
				'intro' => $_POST['intro'],
				'pic'=>$pic
				);
			/*$sql = "insert into users values ('$user[uid]','$user[uname]','$user[password]','$user[gender]','$user[birthdate]','$user[hobby]','$user[degree]','$user[intro]','$user[pic]')";*/
			$sql="update users set uid='$user[uid]',uname='$user[uname]',gender='$user[gender]',birthdate='$user[birthdate]',hobby='$user[hobby]',degree='$user[degree]',intro='$user[intro]',pic='$user[pic]' where uid=".$user['uid'];
			//echo "$sql";
			$result = $this->conn->query($sql);
			return $result;
		}

		function addUser($data){
			//var_dump($data);
			//如果选择了头像，则上传头像文件
			if(!empty($_FILES["pic"]["name"])){
  				$pic = parent::uploadfile('pic','../images/',array('png','jpg','gif'));
  			}
		    else{
		  		$pic='';
		  	}
			$user = array(
				'uid' =>'',
				'uname' => $data['uname'],
				'password' => $data['password'],
				'gender' => $data['gender'],
				'birthdate' => $data['birthdate'],
				'hobby' => implode($data['hobby'],','),
				'degree' => $data['degree'],
				'intro' => $data['intro'],
				'pic' => $pic
				);
			$keys = implode(',',array_keys($user));
			$values = implode("','",array_values($user));
			/*$sql = "insert into users values ('$user[uid]','$user[uname]','$user[password]','$user[gender]','$user[birthdate]','$user[hobby]','$user[degree]','$user[intro]','$user[pic]')";*/
			$sql="insert into users ({$keys}) VALUES ('{$values}')";
			//echo "$sql";
			$result = $this->conn->query($sql);
			return $result;
		}

		function Deluser($id){
			$sql = "Delete from users where uid=".$id;
			$data = $this->conn->query($sql);
			return $data;
		}
	}
 ?>